/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ssau.view;

import ru.ssau.lib.GBC;
import ru.ssau.lib.DateSetPanel;
import ru.ssau.lib.InvalidDateException;
import java.awt.*;
import java.awt.event.*;
import java.util.regex.Pattern;
import javax.swing.*;
import javax.swing.border.Border;
import ru.ssau.entities.AuthorizationInfo;
import ru.ssau.entities.User;
import ru.ssau.lib.InvalidValueException;

/**
 *
 * @author Дима
 */
public class UserDialog extends JDialog {

    private JTextField userNameField;
    private DateSetPanel datePanel;
    private JTextArea userInfoArea;
    private JTextField loginField;
    private JTextField passwordField;

    private JButton okButton;
    private JButton cancelButton;

    private User user = null;

    public UserDialog(Frame parent) {
        super(parent, "Add/edit user", true);
        initComponents();
        layoutComponents();

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                user = null;
                setVisible(false);
            }
        });

        pack();
        setMinimumSize(getPreferredSize());
    }

    public void setLoginString(String loginString) {
        loginField.setText(loginString);
    }

    public String getLogin() throws InvalidValueException {
        boolean isLoginCorrect;
        isLoginCorrect = Pattern.matches("[a-zA-Z_]\\w*", loginField.getText());
        if(!isLoginCorrect) {
            throw new InvalidValueException("Wrong login");
        } else {
            return loginField.getText();
        }
    }

    private void initComponents() {
        userNameField = new JTextField(8);
        datePanel = new DateSetPanel();
        userInfoArea = new JTextArea(2,22);
        userInfoArea.setLineWrap(true);
        loginField = new JTextField(8);
        passwordField = new JTextField(8);

        okButton = new JButton("OK");
        okButton.addActionListener(new OkAction());

        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new CancelAction());
    }

    private void layoutComponents() {
        setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

        JPanel userPanel = new JPanel(new GridBagLayout());
        add(userPanel);
        Border uBorder = BorderFactory.createEtchedBorder();
        userPanel.setBorder(BorderFactory.createTitledBorder(uBorder, "User info"));

        userPanel.add(new JLabel("User name:", JLabel.RIGHT), new GBC(0,0).setInsets(0,0,3,5));
        userPanel.add(userNameField, new GBC(1,0).setInsets(0,0,0,5).setWeight(50,0).setFill(GBC.HORIZONTAL));
        userPanel.add(datePanel, new GBC(0,1,4,3).setFill(GBC.BOTH));
        userPanel.add(new JLabel("User info:", JLabel.RIGHT), new GBC(0,4).setInsets(2,0,5,3));
        userPanel.add(new JScrollPane(userInfoArea), new GBC(1,4,3,2).setInsets(2,2,0,5).setWeight(50,0).setFill(GBC.BOTH));

        JPanel authPanel = new JPanel(new GridBagLayout());
        add(authPanel);
        Border aBorder = BorderFactory.createEtchedBorder();
        authPanel.setBorder(BorderFactory.createTitledBorder(aBorder, "Authorizaytion info"));
        authPanel.add(new JLabel("Login:", JLabel.RIGHT), new GBC(0,0).setInsets(0,0,3,5));
        authPanel.add(loginField, new GBC(1,0).setInsets(0,0,0,5).setWeight(50,0).setFill(GBC.HORIZONTAL));
        authPanel.add(new JLabel("Password:", JLabel.RIGHT), new GBC(2,0).setInsets(3,3,5,5));
        authPanel.add(passwordField, new GBC(3,0).setInsets(3,3,0,5).setWeight(50,0).setFill(GBC.HORIZONTAL));

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);
        add(buttonPanel);
    }

    public User showDialog(User aUser) {
        this.user = aUser;
        if(user == null) {
            user = new User();
        }

        userNameField.setText(user.getName());
        datePanel.setDate(user.getRegistrationDate());
        userInfoArea.setText(user.getInfo());
        loginField.setText(user.getAuthorizationInfo().getLogin());
        passwordField.setText(user.getAuthorizationInfo().getPassword());

        userNameField.grabFocus();
        setVisible(true);
        return this.user;
    }

    private class OkAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            try {
                if(loginField.getText().trim().equals(""))
                    throw new InvalidFieldValueException("Login field is empty");

                if(passwordField.getText().trim().equals(""))
                    throw new InvalidFieldValueException("Password field is empty");

                if(userNameField.getText().trim().equals(""))
                    throw new InvalidFieldValueException("User name field is empty");

                AuthorizationInfo authInfo = new AuthorizationInfo(loginField.getText(), passwordField.getText());
                user.setAuthorizationInfo(authInfo);
                user.setRegistrationDate(datePanel.getDate());
                user.setName(userNameField.getText());
                user.setInfo(userInfoArea.getText());
                setVisible(false);
            } catch(InvalidDateException exception) {
                JOptionPane.showMessageDialog(UserDialog.this, exception.getMessage(), "Input error", JOptionPane.ERROR_MESSAGE);
            } catch(InvalidFieldValueException exception) {
                JOptionPane.showMessageDialog(UserDialog.this, exception.getMessage(), "Input error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private class CancelAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            user = null;
            setVisible(false);
        }
    }
}
